﻿layui.use(['layer', 'tree', 'table', 'laypage', 'laydate', 'jquery'], function () {
    var $ = layui.jquery,
        tree = layui.tree,
        table = layui.table,
        laydate = layui.laydate,
        player = parent.layer || layui.layer;

    var authorityUserListLogic = {
        //初始化
        init: function () {
            self = this;

            this.Parameters = {
                ac: new AsyncCaller(),    //异步方法管理
                sDepartmentCode: "",
                field: 'sLoginName',
                order: 'asc',
                listUrl: "/Authority/User/LoadUserList",
                departmentTreeUrl: "/Authority/User/LoadDepartmentTreeList",
                addUrl: "/Authority/User/Add",
                modifyUrl: "/Authority/User/Modify",
                changePswUrl: "/Authority/User/ChangePwd",
                removeUrl: "/Authority/User/Remove",
            };

            this.Parameters.ac
                .pushQueue(function () {
                    CommonLogic.loadButtonsInfo(function () {
                        self.Parameters.ac.notifyQueueAsyncFuncComplate();
                    })
                })
                .pushQueue(function () {
                    self.initPage();
                    self.Parameters.ac.notifyQueueAsyncFuncComplate();
                })
                .pushQueue(this.bindEvent)
                .exec();

            //this.initPage();
            //this.bindEvent();
        },
        //初始化列表
        initPage: function () {
            //获取部门树状列表信息
            self.logicFunc.loadDepartmentTreeListInfo();

            //限制左侧树高度
            self.logicFunc.departmentHeight();

            //获取用户信息
            var title =
                [{ type: 'checkbox', fixed: 'left' },
                { field: 'gCode', title: 'gCode', hide: true },
                { field: 'sLoginName', title: '登录名', width: 130, align: "center", sort: true, fixed: 'left' },
                { field: 'sUserName', title: '姓名', width: 130, align: "center", sort: true, fixed: 'left' },
                {
                    field: 'iSex', title: '性别', width: 70, align: "center", fixed: 'left',
                    templet: function (d) {
                        return GrdEnum.getValue.userSexValue(d.iSex);
                    }
                },
                { field: 'sRoleNames', title: '角色', width: 150 },
                { field: 'sDepartmentNames', title: '部门', width: 150 },
                { field: 'sPostNames', title: '岗位', width: 150 },
                { field: 'sPhone', title: '联系电话', width: 120, align: "center", sort: true },
                //{
                //    field: 'dCreateTime', title: '创建时间', width: 150, align: "center",
                //    templet: function (d) {
                //        return GrdAssist.formatDate(d.dCreateTime, "yyyy-MM-dd HH:mm:ss");
                //    }
                //},
                {
                    field: 'iState', title: '状态', width: 70, align: "center",
                    templet: function (d) {
                        return GrdEnum.getValue.userStateValue(d.iState);
                    }
                },
                { field: 'opt', title: '操作', toolbar: '#gridTool', width: '220', align: 'left', fixed: 'right' }
                ];

            //table加载
            GrdLayUI.table({
                dom: '#userList',
                url: self.Parameters.listUrl,
                field: self.Parameters.field,
                order: self.Parameters.order,
                parameters: {
                    sKeyWords: "",
                    sDepartmentCode: ""
                },
                title: title
            });

            //排序监听
            GrdLayUI.tableSort({
                domId: "userList",
                backAction: function (obj) {
                    self.Parameters.field = obj.field;
                    self.Parameters.order = obj.type;
                    self.logicFunc.loadListInfo();
                }
            });
        },
        //绑定事件
        bindEvent: function () {
            ///根据网页变化高度或宽度，调整树高度
            window.onresize = function () {
                self.logicFunc.departmentHeight();
            }

            //监听(列表操作)
            table.on('tool(userList)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case "funBtnEdit":
                        self.logicFunc.modifyInfo(data);
                        break;
                    case "funBtnPswEdit":
                        self.logicFunc.changePswInfo(data);
                        break;
                    case "funBtnRemove":
                        self.logicFunc.removeInfo(data);
                        break;
                }
            });

            //查询
            $("#funBtnSearch").on("click", function () {
                self.logicFunc.loadListInfo();
            });

            //新增
            $("#funBtnAdd").on("click", function () {
                self.logicFunc.addInfo();
            });

            //修改
            $("#funBtnEdit").on("click", function () {
                self.logicFunc.modifyInfo();
            });

            //密码
            $("#funBtnPswEdit").on("click", function () {
                self.logicFunc.changePswInfo();
            });

            //删除
            $("#funBtnRemove").on("click", function () {
                self.logicFunc.removeInfo();
            });


        },
        //逻辑方法
        logicFunc: {
            //部门高度
            departmentHeight: function () {
                var clientHeight = document.documentElement.clientHeight * 0.81;
                $(".layui-card-body").css({ "height": clientHeight });
            },

            ///获取数据
            loadListInfo: function () {
                GrdLayUI.tableReload({
                    domId: "userList",
                    field: self.Parameters.field,
                    order: self.Parameters.order,
                    parameters: {
                        sKeyWords: $("#txtKeyWord").val(),
                        sDepartmentCode: self.Parameters.sDepartmentCode
                    }
                });
            },

            ///获取部门树状列表信息
            loadDepartmentTreeListInfo: function () {
                GrdLayUI.ajaxPost({
                    url: self.Parameters.departmentTreeUrl,
                    data: "",
                    success: function (result) {
                        if (result.Code == 1000) {
                            //仅节点左侧图标控制收缩
                            tree.render({
                                elem: '#postTreeList',
                                data: result.Data,
                                onlyIconControl: true, //是否仅允许节点左侧图标控制展开收缩 
                                showLine: false,//是否开启连接线
                                accordion: true,
                                target: '_blank', //是否新选项卡打开（比如节点返回href才有效）
                                skin: 'shihuang',
                                click: function (obj) {
                                    self.Parameters.sDepartmentCode = obj.data.id;
                                    self.logicFunc.loadListInfo();
                                }
                            });

                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            },

            //增加信息
            addInfo: function () {
                var index = layui.layer.open({
                    title: "增加用户信息",
                    type: 2,
                    resize: false,
                    area: ['700px', '700px'],
                    content: self.Parameters.addUrl,
                    end: function () {
                        $(window).unbind("resize");
                    }
                });
                layui.layer.full(index);
            },

            //修改信息
            modifyInfo: function (data) {
                if (data != undefined && data != null) {
                    var index = layui.layer.open({
                        title: "修改用户信息",
                        type: 2,
                        resize: false,
                        area: ['700px', '700px'],
                        content: self.Parameters.modifyUrl + '?data=' + GrdLayUI.zip(data),
                        end: function () {
                            $(window).unbind("resize");
                        }
                    });
                    layui.layer.full(index);
                } else {
                    GrdLayUI.getSingleCheckData({
                        domId: "userList",
                        backAction: function (data) {
                            var index = layui.layer.open({
                                title: "修改用户信息",
                                type: 2,
                                resize: false,
                                area: ['700px', '700px'],
                                content: self.Parameters.modifyUrl + '?data=' + GrdLayUI.zip(data),
                                end: function () {
                                    $(window).unbind("resize");
                                }
                            });
                            layui.layer.full(index);
                        }
                    });

                }
            },

            //修改密码
            changePswInfo: function (data) {
                if (data != undefined && data != null) {
                    //var json = {
                    //    gCode: data.gCode
                    //};
                    var index = layui.layer.open({
                        title: "修改用户密码",
                        type: 2,
                        resize: false,
                        area: ['600px', '400px'],
                        content: self.Parameters.changePswUrl + '?data=' + data.gCode,
                        end: function () {
                            $(window).unbind("resize");
                        }
                    });
                    //layui.layer.full(index);
                } else {
                    GrdLayUI.getSingleCheckData({
                        domId: "userList",
                        backAction: function (data) {
                            var index = layui.layer.open({
                                title: "修改用户密码",
                                type: 2,
                                resize: false,
                                area: ['600px', '400px'],
                                content: self.Parameters.changePswUrl + '?data=' + data.gCode,
                                end: function () {
                                    $(window).unbind("resize");
                                }
                            });
                            //layui.layer.full(index);
                        }
                    });

                }
            },

            //删除按钮信息
            removeInfo: function (data) {
                if (data != undefined && data != null) {
                    GrdLayUI.removeDataByInfo({
                        data: data,
                        removeUrl: self.Parameters.removeUrl,
                        backAction: function (data) {
                            self.logicFunc.loadListInfo();
                        }
                    });
                } else {
                    GrdLayUI.removeData({
                        domId: "userList",
                        removeUrl: self.Parameters.removeUrl,
                        backAction: function (data) {
                            self.logicFunc.loadListInfo();
                        }
                    });
                }
            }
        },
        //工具
        tools: {

        }
    };
    authorityUserListLogic.init();
});
